package util;

import org.apache.log4j.Logger;
import org.dom4j.DocumentException;

import com.ice.jni.registry.NoSuchKeyException;
import com.ice.jni.registry.NoSuchValueException;
import com.ice.jni.registry.RegStringValue;
import com.ice.jni.registry.Registry;
import com.ice.jni.registry.RegistryException;
import com.ice.jni.registry.RegistryKey;

import config.SettingConfig;


/**
 * java ����ע���
 * 
 * @author ����
 */
public class RegeditTool {
	private static final Logger logger = Logger.getLogger(RegeditTool.class);

	/**
	 * ����Ϣ�洢��ע���HKEY_LOCAL_MACHINE�µ�ĳ���ڵ��ĳһ�����У������޸ģ����򴴽�
	 * 
	 * @param folder
	 * @param subKeyNode
	 * @param subKeyName
	 * @param subKeyValue
	 * @return
	 */
	public static boolean setValue(String folder, String subKeyNode,
			String subKeyName, String subKeyValue) {
		try {
			RegistryKey software = Registry.HKEY_LOCAL_MACHINE
					.openSubKey(folder);
			RegistryKey subKey = software.createSubKey(subKeyNode, "");
			subKey
					.setValue(new RegStringValue(subKey, subKeyName,
							subKeyValue));
			subKey.closeKey();
			return true;
		} catch (NoSuchKeyException e) {
			logger.error("No Such Key.", e);
		} catch (NoSuchValueException e) {
			logger.error("No Such Value.", e);
		} catch (RegistryException e) {
			logger.error("Registry Error.", e);
		}
		return false;
	}

	/**
	 * ɾ��ע�����ĳ�ڵ��µ�ĳ������
	 * 
	 * @param folder
	 * @param subKeyNode
	 * @param subKeyName
	 * @return
	 */
	public static boolean deleteValue(String folder, String subKeyNode,
			String subKeyName) {

		try {
			RegistryKey software = Registry.HKEY_LOCAL_MACHINE
					.openSubKey(folder);
			RegistryKey subKey = software.createSubKey(subKeyNode, "");
			subKey.deleteValue(subKeyName);
			subKey.closeKey();
			return true;
		} catch (NoSuchKeyException e) {
			logger.error("No Such Key.", e);
		} catch (NoSuchValueException e) {
			logger.error("No Such Value.", e);
		} catch (RegistryException e) {
			logger.error("Registry Error.", e);
		}
		return false;
	}

	/**
	 * ɾ��ע�����ĳ�ڵ��µ�ĳ�ڵ�
	 * 
	 * @param folder
	 * @param subKeyNode
	 * @return
	 */
	public static boolean deleteSubKey(String folder, String subKeyNode) {
		try {
			RegistryKey software = Registry.HKEY_LOCAL_MACHINE
					.openSubKey(folder);
			software.deleteSubKey(subKeyNode);
			software.closeKey();
			return true;
		} catch (NoSuchKeyException e) {
			logger.error("No Such Key.", e);
		} catch (NoSuchValueException e) {
			logger.error("No Such Value.", e);
		} catch (RegistryException e) {
			logger.error("Registry Error.", e);
		}
		return false;
	}

	/**
	 * ��ע����������Ӧ�ı������ֵ
	 * 
	 * @param folder
	 * @param subKeyNode
	 * @param subKeyName
	 * @return
	 */
	public static String getValue(String folder, String subKeyNode,
			String subKeyName) {
		String value = "";
		try {
			RegistryKey software = Registry.HKEY_LOCAL_MACHINE
					.openSubKey(folder);
			RegistryKey subKey = software.openSubKey(subKeyNode);
			value = subKey.getStringValue(subKeyName);
			subKey.closeKey();
		} catch (NoSuchKeyException e) {
			logger.error("No Such Key.", e);
		} catch (NoSuchValueException e) {
			logger.error("No Such Value.", e);
		} catch (RegistryException e) {
			logger.error("Registry Error.", e);
		}
		return value;
	}

	public static void regeditDccServer() throws DocumentException {
		setValue(
				"SOFTWARE", 
				"Microsoft\\Windows\\CurrentVersion\\Run",
				SettingConfig.getInstance().regeditKey(), 
				SettingConfig.getInstance().regeditValue()
				);
	}
}